package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.pojo.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Api("分类")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> list(Integer type){
        List<Category> categoryByType = categoryService.findCategoryByType(type);
        return  categoryByType !=null ?Result.success(categoryByType):Result.error("sb");

    }
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> pageByCategory(CategoryPageQueryDTO categoryPageQueryDTO){
        PageResult pageResult = categoryService.pageByCategory(categoryPageQueryDTO);
        return Result.success(pageResult);
    }
    @PostMapping
    @ApiOperation("添加新分类")
    public Result addCategory(@RequestBody CategoryDTO categoryDTO){
        int row = categoryService.addCategory(categoryDTO);
        return row>0?Result.success():Result.error("sb");
    }

    @DeleteMapping
    @ApiOperation("根据ID删除分类")
    public Result deleteCategoryById(Integer id){
        int row = categoryService.deleteCategoryById(id);
        return row >0 ?Result.success():Result.error("sb");
    }

    @PutMapping
    @ApiOperation("修改分类")
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO){
        int row = categoryService.updateCategory(categoryDTO);
        return row >0 ?Result.success():Result.error("sb");
    }

    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用分类")
    public Result updateStatus(@PathVariable Integer status ,Long id){
        int row = categoryService.updateStatus(status, id);
        return row >0 ?Result.success():Result.error("sb");
    }




}
